<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 搜索表单 -->
      <meet-search @search="reload" />
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="roleId"
        :columns="columns"
        :datasource="datasource"
        v-model:selection="selection"
        :scroll="{ x: 800 }"
        cache-key="signMeeting"
      >
        <template #toolbar>
          <a-space>
            <a-button type="primary" class="ele-btn-icon" @click="openEdit()">
              <template #icon>
                <plus-outlined />
              </template>
              <span>新建</span>
            </a-button>
            <a-button
              danger
              type="primary"
              class="ele-btn-icon"
              @click="removeBatch"
            >
              <template #icon>
                <delete-outlined />
              </template>
              <span>删除</span>
            </a-button>
          </a-space>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <a-space>
              <a @click="showQrCode(record)">二维码</a>
              <a-divider type="vertical" />
              <a @click="openEdit(record)">修改</a>
              <a-divider type="vertical" />
              <a @click="openSign(record)">签到管理</a>
              <a-divider type="vertical" />
              <a-popconfirm
                placement="topRight"
                title="确定要删除此会议吗？"
                @confirm="remove(record)"
              >
                <a class="ele-text-danger">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </ele-pro-table>
    </a-card>
    <!-- 编辑弹窗 -->
    <meet-edit v-model:visible="showEdit" :data="current" @done="reload" />
    <!-- 报名用户弹窗 -->
    <sign-user v-model:visible="showSign" :data="meetingId" />
    <!-- 二维码弹窗 -->
    <ele-modal
      :width="170"
      :footer="null"
      title="二维码"
      v-model:visible="qrShow"
    >
      <ele-qr-code :value="qrcode" :size="120" />
    </ele-modal>
  </div>
</template>

<script lang="ts" setup>
  import { createVNode, ref } from 'vue';
  import { message, Modal } from 'ant-design-vue/es';
  import {
    PlusOutlined,
    DeleteOutlined,
    ExclamationCircleOutlined
  } from '@ant-design/icons-vue';
  import type { EleProTable } from 'ele-admin-pro/es';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import { messageLoading, toDateString } from 'ele-admin-pro/es';
  import MeetSearch from './components/meeting-search.vue';
  import MeetEdit from './components/meeting-edit.vue';
  import SignUser from './components/sign-user.vue';
  import {
    pageMeeting,
    removeMeeting,
    removeMeetingBatch
  } from '@/api/meeting';
  import type { Meeting, MeetingParam } from '@/api/meeting/model';
  import { HOME_BASE_URL } from '@/config/setting';

  // 表格实例
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);
  const meetingId = ref();
  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      key: 'index',
      width: 48,
      align: 'center',
      fixed: 'left',
      hideInSetting: true,
      customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    },
    {
      title: '会议名称',
      dataIndex: 'title',
      sorter: true,
      showSorterTooltip: false
    },
    {
      title: '会议地址',
      dataIndex: 'room',
      sorter: true,
      showSorterTooltip: false
    },
    {
      title: '会议时间',
      dataIndex: 'meeting_time',
      sorter: true,
      showSorterTooltip: false
    },
    {
      title: '签到时间',
      dataIndex: 'sign_time',
      sorter: true,
      showSorterTooltip: false,
      customRender: ({ text }) =>
        toDateString(text[0], 'MM-dd HH:mm') +
        '/' +
        toDateString(text[1], 'MM-dd HH:mm')
    },
    {
      title: '创建时间',
      dataIndex: 'created_at',
      sorter: true,
      showSorterTooltip: false,
      ellipsis: true,
      customRender: ({ text }) => toDateString(text)
    },
    {
      title: '操作',
      key: 'action',
      width: 240,
      align: 'center'
    }
  ]);

  // 表格选中数据
  const selection = ref<Meeting[]>([]);

  const qrShow = ref(false);
  const qrcode = ref<string>();
  // 当前编辑数据
  const current = ref<Meeting | null>(null);

  // 是否显示编辑弹窗
  const showEdit = ref(false);

  // 是否显示报名弹窗
  const showSign = ref(false);

  // 表格数据源
  const datasource: DatasourceFunction = ({ page, limit, where, orders }) => {
    return pageMeeting({ ...where, ...orders, page, limit });
  };

  /* 搜索 */
  const reload = (where?: MeetingParam) => {
    meetingId.value = null;
    selection.value = [];
    tableRef?.value?.reload({ page: 1, where });
  };

  /* 打开编辑弹窗 */
  const openEdit = (row?: Meeting) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /* 打开签到管理弹窗 */
  const openSign = (row?: Meeting) => {
    meetingId.value = row?.id;
    showSign.value = true;
  };

  const showQrCode = (row?: Meeting) => {
    qrShow.value = true;
    qrcode.value = HOME_BASE_URL + '/pages/attendance/index?id=' + row?.id;
  };
  /* 删除单个 */
  const remove = (row: Meeting) => {
    const hide = messageLoading('请求中..', 0);
    removeMeeting(row.id)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };

  /* 批量删除 */
  const removeBatch = () => {
    if (!selection.value.length) {
      message.error('请至少选择一条数据');
      return;
    }
    Modal.confirm({
      title: '提示',
      content: '确定要删除选中的会议吗?',
      icon: createVNode(ExclamationCircleOutlined),
      maskClosable: true,
      onOk: () => {
        const hide = messageLoading('请求中..', 0);
        removeMeetingBatch(selection.value.map((d) => d.id))
          .then((msg) => {
            hide();
            message.success(msg);
            reload();
          })
          .catch((e) => {
            hide();
            message.error(e.message);
          });
      }
    });
  };
</script>

<script lang="ts">
  export default {
    name: 'SignMeeting'
  };
</script>
